package com.kexilo.core.security.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 刷新令牌表 kx_refresh_token
 *
 * @author Kexilo
 */
@TableName("kx_refresh_token")
public class RefreshToken implements Serializable {

    private static final long serialVersionUID = 1L;

    /** 主键ID */
    @TableId(type = IdType.AUTO)
    private Long id;

    /** 用户ID */
    private Long userId;

    /** 刷新令牌 */
    private String refreshToken;

    /** 访问令牌 */
    private String accessToken;

    /** 设备标识 */
    private String deviceId;

    /** 用户代理 */
    private String userAgent;

    /** IP地址 */
    private String ipAddress;

    /** 是否已使用（0：未使用，1：已使用） */
    private String used;

    /** 是否已撤销（0：未撤销，1：已撤销） */
    private String revoked;

    /** 创建时间 */
    private LocalDateTime createTime;

    /** 过期时间 */
    private LocalDateTime expireTime;

    /** 最后使用时间 */
    private LocalDateTime lastUsedTime;

    /** 使用次数 */
    private Integer useCount;

    public RefreshToken() {
    }

    public RefreshToken(Long userId, String refreshToken, String accessToken, LocalDateTime expireTime) {
        this.userId = userId;
        this.refreshToken = refreshToken;
        this.accessToken = accessToken;
        this.expireTime = expireTime;
        this.createTime = LocalDateTime.now();
        this.used = "0";
        this.revoked = "0";
        this.useCount = 0;
    }

    // Getter and Setter methods
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getRefreshToken() {
        return refreshToken;
    }

    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getUserAgent() {
        return userAgent;
    }

    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }

    public String getIpAddress() {
        return ipAddress;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    public String getUsed() {
        return used;
    }

    public void setUsed(String used) {
        this.used = used;
    }

    public String getRevoked() {
        return revoked;
    }

    public void setRevoked(String revoked) {
        this.revoked = revoked;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(LocalDateTime expireTime) {
        this.expireTime = expireTime;
    }

    public LocalDateTime getLastUsedTime() {
        return lastUsedTime;
    }

    public void setLastUsedTime(LocalDateTime lastUsedTime) {
        this.lastUsedTime = lastUsedTime;
    }

    public Integer getUseCount() {
        return useCount;
    }

    public void setUseCount(Integer useCount) {
        this.useCount = useCount;
    }

    /**
     * 判断是否已过期
     */
    public boolean isExpired() {
        return expireTime != null && LocalDateTime.now().isAfter(expireTime);
    }

    /**
     * 判断是否已使用
     */
    public boolean isUsed() {
        return "1".equals(this.used);
    }

    /**
     * 判断是否已撤销
     */
    public boolean isRevoked() {
        return "1".equals(this.revoked);
    }

    /**
     * 判断是否有效
     */
    public boolean isValid() {
        return !isExpired() && !isUsed() && !isRevoked();
    }
}
